<?php

class ProductRepository {

    var $db;

    function ProductRepository() {
        global $SERVER, $DATABASE, $USERNAME, $PASSWORD;
        $this->db = new Connection($SERVER, $DATABASE, $USERNAME, $PASSWORD);
    }
    
    function GetProduct($code)
    {
        $query = "SELECT * FROM product WHERE Code_s = '" . $code . "'";
        $result = $this->db->ExecuteQuery($query) or die("Query failed with error: " . mysql_error());
        if ($row = mysql_fetch_array($result)) {
            return new ProductEntity($row[0], $row[1], $row[2], $row[3], $row[4], $row[5], $row[6],
                            $row[7], $row[8]);
        }
        return null;
    }
    
    function GetAllPager($curPage, $itemsPerPage, &$totalItems){
        $query = "select * from product limit " . ($curPage - 1) * $itemsPerPage . ", " . $itemsPerPage;
        $queryCount = "select count(*) as total from product ";
        if ($queryCountResult = mysql_fetch_assoc($this->db->ExecuteQuery($queryCount)))
            $totalItems = $queryCountResult['total'];
        return $this->ChangeToArray($this->db->ExecuteQuery($query));
    }
    
    function GetProductsBySectionIDPager($sectionID, $curPage, $itemsPerPage, &$totalItems)
    {
        $query = "select * from product " . " where (select SectionID_i from Category where ID_i = product.CategoryID_i) = " . $sectionID . 
            " limit " . ($curPage - 1) * $itemsPerPage . ", " . $itemsPerPage;
        $queryCount = "select count(*) as total from product". " where 
            (select SectionID_i from Category where ID_i = product.CategoryID_i) = " . $sectionID;
        if ($queryCountResult = mysql_fetch_assoc($this->db->ExecuteQuery($queryCount)))
            $totalItems = $queryCountResult['total'];
        return $this->ChangeToArray($this->db->ExecuteQuery($query));
    }

    function SearchProductPager($curPage, $itemsPerPage, $arrCategoryID, $arrColor, $productName, &$totalItems) {
        $query = "select * from product ";
        $queryCount = "select count(*) as total from product ";
        $where = "where ";
        if (count($arrColor) > 0) {
            for ($i = 0; $i < count($arrColor); $i++) {
                $where .= "Color_s like \"%" . $arrColor[$i] . "%\"";
                if ($i < count($arrColor) - 1) {
                    $where .= " or ";
                }
            }
        }
        if (count($arrCategoryID) > 0) {
            if ($where != "where ") {
                $where .= " and ";
            }
            for ($i = 0; $i < count($arrCategoryID); $i++) {
                $where .= "CategoryID_i = \"" . $arrCategoryID[$i] . "\"";
                if ($i < count($arrCategoryID) - 1) {
                    $where .= " or ";
                }
            }
        }
        if ($productName != "") {
            if ($where != "where ") {
                $where .= " and ";
            }
            $where .= "Code_s like \"%" . $productName . "%\"";
        }
        if ($where != "where ") {
            $query .= $where;
            $queryCount .= $where;
        }
        if ($queryCountResult = mysql_fetch_assoc($this->db->ExecuteQuery($queryCount)))
            $totalItems = $queryCountResult['total'];
        $query .= " limit " . ($curPage - 1) * $itemsPerPage . ", " . $itemsPerPage;
        return $this->ChangeToArray($this->db->ExecuteQuery($query));
    }

    function ChangeToArray($result) {
        $array = array();
        $count = 0;
        while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
            $entity = new ProductEntity($row[0], $row[1], $row[2], $row[3], $row[4], $row[5], $row[6],
                            $row[7], $row[8]);
            $array[$count++] = $entity;
        }
        return $array;
    }

    function Insert(ProductEntity $entity)
    {
        $query = "INSERT INTO product(Code_s, Name_s, Description_s, ImageSrc_s, Color_s, PostedDay_dt, Price_f, Bought_i, CategoryID_i) VALUES(";
        $query.="'".$entity->Code_s."',";
        $query.="'".$entity->Name_s."',";
        $query.="'".$entity->Description_s."',";
        $query.="'".$entity->ImageSrc_s."',";
        $query.="'".$entity->Color_s."',";
        $query.="'".$entity->PostedDay_dt."',";
        $query.="'".$entity->Price_f."',";
        $query.="'".$entity->Bought_i."',";
        $query.="'".$entity->CategoryID_i."')";
        return $this->db->ExecuteQuery($query);
    }
    
    function Update(ProductEntity $entity){
        $query = "select * from product where Code_s='" . $entity->Code_s . "'";
        if(mysql_fetch_array($this->db->ExecuteQuery($query))){
            $query = "UPDATE product SET ";
            $query .= "Name_s='" . $entity->Name_s . "',";
            $query .= "Description_s='" . $entity->Description_s . "',";
            $query .= "ImageSrc_s='" . $entity->ImageSrc_s . "',";
            $query .= "Color_s='" . $entity->Color_s . "',";
            $query .= "Price_f=" . $entity->Price_f . ",";
            $query .= "CategoryID_i=" . $entity->CategoryID_i;
            $query .= " WHERE Code_s='" . $entity->Code_s . "'";
            return $this->db->ExecuteQuery($query);
        }
        return 0;
    }
    
    function Delete($code){
        $query = "select * from product where Code_s='" . $code . "'";
        if(mysql_fetch_array($this->db->ExecuteQuery($query))){
            $query = "delete from product where Code_s='" . $code . "'";
            return $this->db->ExecuteQuery($query);
        }
        return 0;
    }
    //------Duong-----
    public function GetDetail($id) {
        $query = "SELECT * FROM product WHERE Code_s LIKE '" . $id . "'";
        $result = $this->db->ExecuteQuery($query) or die("Query failed with error: " . mysql_error());
        if ($result) {
            $row = mysql_fetch_array($result);
            return new ProductEntity($row["Code_s"], $row["Name_s"],
                    $row["Description_s"], $row["ImageSrc_s"], $row["Color_s"],
                    $row["PostedDay_dt"], $row["Price_f"], $row["Bought_i"], $row["CategoryID_i"]);
        }
        return null;
    }

    public function GetCommentsByProductID($productID) {
        $query = "SELECT * FROM comment WHERE ProductCode_s LIKE '" . $productID . "'";
        $result = $this->db->ExecuteQuery($query);
        $array = array();
        $count = 0;
        while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
            $entity = new CommentEntity($row[0], $row[1],
                            $row[2], $row[3], $row[4],
                            $row[5]);
            $array[$count++] = $entity;
        }
        return $array;
    }

    public function InsertComment($name, $email, $subject, $content, $productcode) {
        $query = "INSERT INTO comment(Name_s,Email_s,Subject_s,Content_s,ProductCode_s) VALUES(";
        $query.="'".$name."',";
        $query.="'".$email."',";
        $query.="'".$subject."',";
        $query.="'".$content."',";
        $query.="'".$productcode."')";
        return mysql_query($query);
    }

    //------Duong-----
    //---------------------Vu----------------------------
    public function GetNewProductList($cur ,$itemPer ,&$totalItem) {
        $query = "select * from product order by PostedDay_dt desc limit " . ($cur - 1) * $itemPer. ", " . $itemPer;
        $queryTotal = "select count(*) as total from product ";
        $result = $this->db->ExecuteQuery($query) or die("Failed :" . mysql_error());
        $array = array();
        $count = 0;
        if(($queryCountResult = mysql_fetch_array($this->db->ExecuteQuery($queryTotal)))== true)
        $totalItem = $queryCountResult['total'];
        while ($row = mysql_fetch_array($result)) {
            $entity = new ProductEntity($row[0], $row[1], $row[2], $row[3], $row[4], $row[5], $row[6],
                            $row[7], $row[8]);
            $array[$count++] = $entity;
        }
        return $array;
    }

    public function GetTop9Product() {
        $query = "select * from product order by PostedDay_dt desc limit 0,9 ";
        $result = $this->db->ExecuteQuery($query) or die("Failed :" . mysql_error());
        $array = array();
        $count = 0;
        while ($row = mysql_fetch_array($result)) {
            $entity = new ProductEntity($row[0], $row[1], $row[2], $row[3], $row[4], $row[5], $row[6],
                            $row[7], $row[8]);
            $array[$count++] = $entity;
        }
        return $array;
    }

    public function GetMostSoldProductList($cur,$itemPer ,&$totalItem) {
        $query = "SELECT * FROM product ORDER BY Bought_i DESC  limit ". ($cur - 1) * $itemPer. ", " . $itemPer;;
        $queryTotal = "select count(*) as total from product ";
        $result = $this->db->ExecuteQuery($query) or die("Failed :" . mysql_error());
        $array = array();
        $count = 0;
         if(($queryCountResult = mysql_fetch_array($this->db->ExecuteQuery($queryTotal)))== true)
        $totalItem = $queryCountResult['total'];
        while ($row = mysql_fetch_array($result)) {
            $entity = new ProductEntity($row[0], $row[1], $row[2], $row[3], $row[4], $row[5], $row[6],
                            $row[7], $row[8]);
            $array[$count++] = $entity;
        }
        return $array;
    }
    
   public function GetProductFromCategory($id ,$cur ,$itemPer ,&$totalItem) {
         $query = "select * from product where CategoryID_i = ".$id." limit " . ($cur - 1) * $itemPer. ", " . $itemPer;
        $queryTotal = "select count(*) as total from product where CategoryID_i = ".$id;
        $result = $this->db->ExecuteQuery($query) or die("Failed :" . mysql_error());
        $array = array();
        $count = 0;
        if(($queryCountResult = mysql_fetch_array($this->db->ExecuteQuery($queryTotal)))== true)
        $totalItem = $queryCountResult['total'];
        while ($row = mysql_fetch_array($result)) {
            $entity = new ProductEntity($row[0], $row[1], $row[2], $row[3], $row[4], $row[5], $row[6],
                            $row[7], $row[8]);
            $array[$count++] = $entity;
        }
        return $array;
    }


    
}

?>